package sliver.action.shop.ajax;

import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import sliver.action.shop.ajax.entry.ReturnGoods;
import sliver.entity.Goods;
import sliver.service.ShopManager;

import com.google.gson.Gson;
import com.opensymphony.xwork2.ActionSupport;

@Scope("prototype")
@Controller("shop.ajax.queryShopGoodsAction")
public class QueryShopGoodsAction extends ActionSupport{

	private static final long serialVersionUID = -3805049990442029343L;
	
	private Integer shopperId;
	private String username;
	private String password;
	
	@Autowired
	private ShopManager shopManager;
	
	public QueryShopGoodsAction(){
		
	}

	public Integer getShopperId() {
		return shopperId;
	}

	public void setShopperId(Integer shopperId) {
		this.shopperId = shopperId;
	}
	
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Override
	public String execute() throws Exception {
		
		List<Goods> goods = shopManager.listMyGoods(shopperId);
		
		Gson gson = new Gson();
		List<ReturnGoods> returnGoods = new ArrayList<ReturnGoods>();
		
		ReturnGoods temp = null;
		if(goods != null && goods.size() > 0){
			for(Goods g : goods){
				temp = new ReturnGoods();
				temp.setId(g.getId());
				temp.setName(g.getName());
				temp.setPrice(g.getPrice());
				temp.setDescription(g.getDescription());
				temp.setSize(g.getSizes());
				temp.setMinImagePath(g.getMinImagePath());
				temp.setMidImagePath(g.getMidImagePath());
				temp.setMaxImagePath(g.getMaxImagePath());
				returnGoods.add(temp);
			}
			
		}
		
		
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json;charset=utf-8");
		
		Writer writer = response.getWriter();
		writer.write(gson.toJson(returnGoods));
		writer.flush();
		
		return null;
	}
	
}
